package com.jmp.api.controller;

import com.jmp.base.*;
import com.jmp.base.dto.SmsCaptchaCheckRequest;
import com.jmp.base.dto.SmsCaptchaCheckResponse;
import com.jmp.base.dto.SmsCaptchaRequest;
import com.jmp.base.dto.SmsSendRequest;
import com.jmp.sms.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cjr
 */
@Api(description = "短信")
@RestController
@Slf4j
public class SmsController implements SmsApi {

    @Autowired
    private CaptchaService captchaService;

    @Override
    @ApiOperation(value = "发送短信")
    public ResultModel sendBatchSms(@RequestBody SmsSendRequest request) {
        captchaService.sendBatch(request.getPhones(), request.getType(), request.getParams());
        return ResultModel.success();
    }

    @Override
    @ApiOperation(value = "发送验证码")
    public ResultModel captchaSend(@RequestBody SmsCaptchaRequest request) {
        captchaService.send(request.getPhone(), request.getType(), request.getIp(), request.getLength());
        return ResultModel.success();
    }

    @Override
    @ApiOperation(value = "校验验证码")
    public ResultModel<SmsCaptchaCheckResponse> captchaCheck(@RequestBody SmsCaptchaCheckRequest request) {
        boolean isSuccess = captchaService.check(request.getPhone(), request.getType(), request.getCaptcha());
        SmsCaptchaCheckResponse response = SmsCaptchaCheckResponse.builder()
                .success(isSuccess)
                .build();
        return ResultModel.success(response);
    }
}
